Classification with a CNN


Classification with a CNN

A convolutional neural network is primarily used for image classification. However, in this section will use a convolutional neural network to classify binary numbers.
Una red neuronal convolucional es primariamente usada para clasificación de imágenes. Sin embargo, en esta sección usaremos una red neuronal convolucional para clasificar números en binario.

Problem 1
Repeat the problem in Classification > Numeric Application to classify numbers that are divisible by 12, divisible by 20 or prime using a convolutional neural network and SoftMax. Create a new project, select Convolutional Neural Network, Classification and Genetic Algorithm in the new project dialog. Also check the options to generate the files BuildTrainSet.lab and BuildValidSet.lab. The project name must be Conv1220.
Repita el problema en Classification > Numeric Application para clasificar números que son divisibles entre 12, divisibles entre 10 o primo usando una red neuronal convolucional y SoftMax. Cree un nuevo proyecto, seleccione Convolutional Neural Network, Classification y Genetic Algorithm en el diálogo de nuevo proyecto. También seleccione las opciones para generar los archivos BuildTrainSet.lab y BuildValidSet.lab. El nombre del proyecto debe ser Conv1220.

Classification

Step A
Edit the BuildTrainSet.lab file to build an appropriate training set for the classification of the numbers. Use the number from 0 to 511 (9 bits). The input training set must include 16 times each number. The classifier must be able to tolerate 10% of noise in the inputs. If a number is divisible by 12 and by 20, set the class to "class one".
Edite el archivo BuildTrainSet.lab para construir un conjunto de datos apropiado para la clasificación de los números. Use los números desde 0 a 511 (9 bits). La entrada del conjunto de datos de entrenamiento debe incluir 16 veces cada número. El clasificador debe ser capaz de tolerar 10% de ruido en las entradas. Si un número es divisible entre 12 y entre 20, fije la clase a "la clase uno".

Conv1220\BuildTrainSet.lab
Matrix trainSetTarget;
trainSetTarget.Create(512, 3); // Class1: divisible by 12, Class 2: divisible by 20, Class 3: prime

Matrix input;
Matrix column;
Matrix row;
Vector number;
int i = 0;
for(i = 0; i < 512; i++)
{
     number.CreateBinary(9, i); // Convert to binary
     column = number; // Binary number in one column
     row = column.Transpose(); // Binary number in one row
     input.AppendDown(row);
     if (i%12 == 0 && i != 0) // Is divisible by 12 (class 1)?
     {
          trainSetTarget[i][0] = 1;
     }
     else if (i%20 == 0 && i != 0) // Is divisible by 20 (class 2)?
     {
          trainSetTarget[i][1] = 1;
     }
     else if (i%12 == 0 && i%20 == 0 && i != 0) // Is divisible by 12 and 20 force class 1?
     {
          trainSetTarget[i][0] = 1;
          trainSetTarget[i][1] = 0;
     }
     else if (isprime(i) == true) // Is prime (class 3)?
     {
          trainSetTarget[i][2] = 1;
     }
}
input.AppendDown(input);
input.AppendDown(input);
input.AppendDown(input);
input.AppendDown(input);
input.Save(); // Save the input to create the validation set
//
Matrix noise;
noise.CreateRandom(input.GetRowCount(), input.GetColCount(), 0.0, 1.0);
Matrix trainSetInput = 0.9*input + 0.1*noise;
trainSetInput.Save();
//
trainSetTarget.AppendDown(trainSetTarget);
trainSetTarget.AppendDown(trainSetTarget);
trainSetTarget.AppendDown(trainSetTarget);
trainSetTarget.AppendDown(trainSetTarget);
trainSetTarget.Save();

input

trainSetInput

trainSetTarget

Step B
Edit the BuildValidSet.lab file, then execute the code.
Edite el archivo BuildValidSet.lab, entonces ejecute el código.

Conv1220\BuildValidSet.lab
Matrix input;
input.Load();
//
Matrix noise;
noise.CreateRandom(input.GetRowCount(), input.GetColCount(), 0.0, 1.0);
Matrix validSetInput = 0.9*input + 0.1*noise;
validSetInput.Save();

validSetInput

Step C
Edit the Train.lab file, then execute the code.
Edite el archivo Train.lab, entonces ejecute el código.

Conv1220\Train.lab
//_______________________________________ 1. Network setup
ConvNet net;
net.Create(1, 1, 9, 2);
//_______________________________________ 2. Scaling
net.SetInRange(0.0, 1.0);
net.SetOutRange(0.0, 1.0);
//_______________________________________ 3. Layer setup
net.SetFullLayer(0, 1, 20); // logsig=1, 20 neurons
net.SetFullLayer(1, 5, 3); // SoftMaxReject=5, 3 neurons
//_______________________________________ 4. Load and set the training set
Tensor trainSetInput;
trainSetInput.LoadCsv(9, 1, 1);
Tensor trainSetTarget;
trainSetTarget.LoadCsv(3, 1, 1);
net.SetTrainSet(trainSetInput, trainSetTarget, false);
//_______________________________________ 5. Train using a genetic algoritm (logsig)
net.TrainGenetic(
     100, //Population Size
     80, //Number Generations
     1.8, //Over Population
     0.001, //Probability of Mutation
     0.8, //Probability of Crossover
     1.0e-5, //Goal (desired mse)
     false //Use Singular Value Decomposition
);
//_______________________________________ 6. Train using conjugate gradient (SoftMax)
net.TrainConjGrad(2500,1.0e-10);
//_______________________________________ 7. Save the trained network
net.Save();

Step D
Edit the CheckTrain.lab file, then execute the code to check the training.
Edite el archivo CheckTrain.lab, entonces ejecute el código para verificar el entrenamiento.

Conv1220\CheckTrain.lab
//_________________________________________ 1. Load the Training Set
Tensor trainSetInput;
trainSetInput.LoadCsv(9, 1, 1);
Tensor trainSetTarget;
trainSetTarget.LoadCsv(3, 1, 1);
//_________________________________________ 2. Load the network
ConvNet net;
net.Load();
//_________________________________________ 3. Run
Tensor output;
net.Run(trainSetInput, output);
//_________________________________________ 4. Compute the Confusion Matrix
Matrix trainConf;
output.ConfusionMatrix(trainSetTarget, 0.5, trainConf);
trainConf.Save();
//_________________________________________ Compute the Number of Errors
int numErrors = toint(trainConf.GetSum()) - toint(trainConf.GetDiagonalSum());

CheckTrain

Step E
Edit the Validation.lab file, then execute the code to validate the performance of the network.
Edite el archivo CheckTrain.lab, entonces ejecute el código para validar el desempeño de la red neuronal.

Conv1220\Validation.lab
//_________________________________________ 1. Load the validation set
Tensor validSetInput;
validSetInput.LoadCsv(9, 1, 1);
Tensor trainSetTarget;
trainSetTarget.LoadCsv(3, 1, 1);
//_________________________________________ 2. Load the ANN
ConvNet net;
net.Load();
//_________________________________________ 3. Run
Tensor output;
net.Run(validSetInput, output);
//_________________________________________ 4. Compute the confusion matrix
Matrix validConf;
output.ConfusionMatrix(trainSetTarget, 0.5, validConf);
validConf.Save();
//_________________________________________ 5. Compute the Number of Errors
int numErrors = toint(validConf.GetSum()) - toint(validConf.GetDiagonalSum());

Validation

Step F
Add a new file called TrainX.lab to use the logsig function during the training with Simulated Annealing, and then, use SoftMax (in the output layer) during the training with the Conjugate Gradient method. Observe that the training set MUST BE set again after changing the activation function in order to re-scaled the data.
Agregue un archivo nuevo llamado TrainX.lag para usar la función logsig durante el entrenamiento con el Templado Simulado, y entonces, usar SoftMax (en la capa de salida) durante el entrenamiento con el método del Gradiente Conjugado. Observe que el conjunto de datos de entrenamiento DEBE SER fijado otra vez después de cambiar la función de activación a fin de re-escalar los datos.

Conv1220\TrainX.lab
//_______________________________________ 1. Network setup
ConvNet net;
net.Create(1, 1, 9, 2);
//_______________________________________ 2. Scaling
net.SetInRange(0.0, 1.0);
net.SetOutRange(0.0, 1.0);
//_______________________________________ 3. Layer setup
net.SetFullLayer(0, 1, 20); // logsig=2, 20 neurons
net.SetFullLayer(1, 1, 3); // logsig=1, 3 neurons
//_______________________________________ 4. Load and set the training set
Tensor trainSetInput;
trainSetInput.LoadCsv(9, 1, 1);
Tensor trainSetTarget;
trainSetTarget.LoadCsv(3, 1, 1);
net.SetTrainSet(trainSetInput, trainSetTarget, false);
//_______________________________________ 5. Train simulated annealing (logsig)
net.TrainSimAnneal(
     20, //Number of Temperatures
     20, //Number Iterations
     15, //Initial Temperature
     0.001, //Final Temperature
     false, // Is Cooling Schedule Linear?
     2, // Number of Cycles
     1.0e-6, // Goal (desired mse)
     true // Use Singular Value Decomposition
);
//_______________________________________ 6. Train using conjugate gradient (SoftMax)
net.SetActiFunc(1, 5);
net.SetTrainSet(trainSetInput, trainSetTarget, false);
net.TrainConjGrad(2500,1.0e-10);
//_______________________________________ 7. Save the trained network
net.Save();

Problem 2
What do you recommend to reduce the number of errors during validation?
Que recomendaría usted para reducir el número de errores durante la validación?

© Copyright 2000-2021 Wintempla selo. All Rights Reserved. Jul 22 2021. Home